package com.beep.erp.common.widget.tips;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.beep.erp.R;
import com.beep.erp.common.utils.DensityUtils;


/**
 * FileName: DefaultTipsHelper
 * description:
 * Author: jolly
 * Date: 2016/8/26
 */
public class DefaultTipsHelper implements TipsHelper {

    protected final ProgressBar mLoadingView;
    protected final View mView;
    private Activity mContext;

    public DefaultTipsHelper(Context context,View view) {
        mContext= (Activity) context;
       this.mView=view;
        mLoadingView = new ProgressBar(context);
//        mLoadingView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//        mLoadingView.setImageResource(R.drawable.spinner);
        mLoadingView.setPadding(0, (int) DensityUtils.dp2px(context, 10),
                0, (int) DensityUtils.dp2px(context, 10));
        mLoadingView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                (int) DensityUtils.dp2px(context, 40)));
    }

    @Override
    public void showEmpty() {
        hideLoading();
        TipsUtils.showTips(mView, TipsType.EMPTY);
    }

    @Override
    public void showEmptyType(String message) {
        View tipsView = TipsUtils.showTips(mView, TipsType.EMPTYTYPE);
        if (!TextUtils.isEmpty(message)) {
            ((TextView) tipsView.findViewById(R.id.description)).setText(message);
        }
    }

    @Override
    public void hideEmptyType() {
        TipsUtils.hideTips(mView, TipsType.EMPTYTYPE);
    }


    @Override
    public void hideEmpty() {
        TipsUtils.hideTips(mView, TipsType.EMPTY);
    }




    @Override
    public void showLoading(boolean firstPage) {
        hideEmpty();
        hideError();
        if (firstPage) {
           TipsUtils.showTips(mView, TipsType.LOADING);
//            AnimationDrawable drawable = (AnimationDrawable) ((ImageView) tipsView).getDrawable();
//            drawable.start();
          //  return;
        }
    }

    @Override
    public void hideLoading() {
        hideEmpty();
        hideError();
        TipsUtils.hideTips(mView, TipsType.LOADING);
    }

    @Override
    public void showError(boolean firstPage, String errorMessage, View.OnClickListener onClickListener) {
        hideLoading();
        if (firstPage) {
            View tipsView = TipsUtils.showTips(mView, TipsType.LOADING_FAILED);
            tipsView.findViewById(R.id.retry_btn).setOnClickListener(onClickListener);
            if (!TextUtils.isEmpty(errorMessage)) {
                ((TextView) tipsView.findViewById(R.id.description)).setText(errorMessage);
            }
            return;
        }

        Toast.makeText(mLoadingView.getContext(), errorMessage, Toast.LENGTH_LONG).show();
    }

    @Override
    public void hideError() {
        TipsUtils.hideTips(mView, TipsType.LOADING_FAILED);
    }

    @Override
    public void showHasMore() {
//        if (!mFragment.getHeaderAdapter().containsFooterView(mLoadingView)) {
//            ((AnimationDrawable) mLoadingView.getDrawable()).start();
//            mFragment.getHeaderAdapter().addFooterView(mLoadingView);
//        }
    }

    @Override
    public void hideHasMore() {
       // mFragment.getHeaderAdapter().removeFooterView(mLoadingView);
    }

}
